home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FIX24.CC < prev    next >
C/C++ Source or Header  |  1992-03-30  |  9KB  |  328 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Kurt Baudendistel (gt-eedsp!baud@gatech.edu)
  5.     adapted for libg++ by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of the GNU C++ Library.  This library is free
  8. software; you can redistribute it and/or modify it under the terms of
  9. the GNU Library General Public License as published by the Free
  10. Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  This library is distributed in the hope
  12. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. PURPOSE.  See the GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. //
  21. // Fix24.cc : fixed precision class support functions
  22. //
  23.  
  24. #ifdef __GNUG__
  25. #pragma implementation "Fix24.h"
  26. #endif
  27. #include <Fix24.h>
  28.  
  29. // basic operators too large to be inline
  30.  
  31. long Fix24::assign(double d) 
  32.   if (d == 1.0)
  33.     return Fix24_m_max;
  34.   else if (d > Fix24_max)
  35.   {
  36.     long i = Fix24_m_max;
  37.     range_error(i);
  38.     return i;
  39.   }
  40.   else if (d < Fix24_min)
  41.   {
  42.     long i = Fix24_m_min;
  43.     range_error(i);
  44.     return i;
  45.   }
  46.   else {
  47.     d = (long) (d * (1 << 24) + ((d >= 0)? 0.5 : -0.5)); // Round to 24 bits
  48.     return ((long) d) << (Fix24_shift - 24);     /* Convert to integer format */
  49.   }
  50. }
  51.  
  52. twolongs Fix48::assign(double d) 
  53.   if (d == 1.0)
  54.     return Fix48_m_max;
  55.   else if (d > Fix48_max)
  56.   {
  57.     twolongs i = Fix48_m_max;
  58.     range_error(i);
  59.     return i;
  60.   }
  61.   else if (d < Fix48_min)
  62.   {
  63.     twolongs i = Fix48_m_min;
  64.     range_error(i);
  65.     return i;
  66.   }
  67.   else {
  68.     twolongs i;
  69.     int sign = (d < 0);
  70.  
  71. /* First, convert the absolute value of d to a 48-bit integer format */
  72.     if (d < 0) d = -d;
  73.     i.u = ((long)(d *= Fix24_mult)) & 0xffffff00;
  74.     i.l = ((unsigned long)((d - i.u)* (Fix24_mult / (1 << 7)))) & 0xffffff00;
  75.  
  76. /* Calculate the two's complement if d was negative */
  77.     if (sign) {
  78.     unsigned long oldlower = i.l;
  79.     i.l = (~i.l + 1) & 0xffffff00;
  80.     i.u = (~i.u + (((oldlower ^ i.l) & Fix24_msb)? 0 : 1)) & 0xffffff00;
  81.     }
  82.     return i;
  83.   }
  84. }
  85.  
  86.  
  87. Fix48 operator * (Fix24& a, Fix24& b)
  88. {
  89. // break a and b into lo and hi parts, and do a multiple-precision
  90. // multiply, with rounding
  91.  
  92.   int apos = (a.m >= 0);
  93.   unsigned long ua = (apos)? a.m : - a.m;
  94.   ua <<= 1; // ua is biased so result will be 47 bit mantissa, not 46:
  95.   unsigned long hi_a = (ua >> 16) & ((1 << 16) - 1);
  96.   unsigned long lo_a = ua & ((1 << 16) - 1);
  97.  
  98.   int bpos = (b.m >= 0);
  99.   unsigned long ub = (bpos)? b.m : -b.m;
  100.   unsigned long hi_b = (ub >> 16) & ((1 << 16) - 1);
  101.   unsigned long lo_b = ub & ((1 << 16) - 1);
  102.  
  103.   unsigned long 
  104.     hi_r = hi_a * hi_b,
  105.     mi_r = hi_a * lo_b + lo_a * hi_b,
  106.     lo_r = lo_a * lo_b,
  107.     rl = ((hi_r << 16) & 0x00ffffffL) + (mi_r & 0x00ffffffL) + (lo_r >> 16);
  108.   twolongs r;
  109.   r.u = (hi_r & 0xffffff00L) + ((mi_r >> 16) & 0x0000ff00L)
  110.     + ((rl >> 16) & 0x0000ff00L);
  111.   r.l = rl << 8;
  112.  
  113.   if ( apos != bpos ) {
  114.     unsigned long l = r.l;
  115.     r.l = -r.l;
  116.     r.u = (~r.u + ((l ^ r.l) & Fix24_msb ? 0 : Fix24_lsb)) & 0xffffff00;
  117.   }
  118.   return r;
  119. }
  120.  
  121. Fix24 operator / (Fix24& a, Fix24& b)
  122. {
  123.   long q;
  124.   int apos = (a.m >= 0);
  125.   unsigned long la = (apos)? a.m : -a.m;
  126.   int bpos = (b.m >= 0);
  127.   unsigned long lb = (bpos)? b.m: -b.m;
  128.   if (la >= lb)
  129.   {
  130.     q = (apos == bpos)? Fix24_m_max: Fix24_m_min;
  131.     a.range_error(q);
  132.   }
  133.   else                        // standard shift-based division alg
  134.   {
  135.     q = 0;
  136.     long r = la;
  137.  
  138.     for (int i = 32; i > 0; i--)
  139.     {
  140.     if ((unsigned)(r) > lb) {
  141.         q = (q << 1) | 1;
  142.         r -= lb;
  143.     }
  144.     else
  145.         q = (q << 1);
  146.     r <<= 1;
  147.     }
  148.  
  149.     q += 0x80;            // Round result to 24 bits
  150.     if (apos != bpos) q = -q;    // Fix sign
  151.   }
  152.   return (q & 0xffffff00);
  153. }
  154.  
  155.  
  156. Fix48 operator + (Fix48&  f, Fix48&  g)
  157. {
  158.   long lo_r = (f.m.l >> 8) + (g.m.l >> 8);
  159.   twolongs r;
  160.   r.u = f.m.u + g.m.u + (lo_r & 0x01000000L ? 0x00000100L : 0);
  161.   r.l =  lo_r << 8;
  162.  
  163.   if ( (f.m.u ^ r.u) & (g.m.u ^ r.u) & Fix24_msb )
  164.     f.overflow(r);
  165.   return r;
  166. }
  167.  
  168. Fix48 operator - (Fix48&  f, Fix48&  g)
  169. {
  170.   unsigned lo_r = (f.m.l >> 8) - (g.m.l >> 8);
  171.   twolongs r;
  172.   r.u = f.m.u - g.m.u - (lo_r & 0x01000000L ? 0x00000100L: 0);
  173.   r.l = lo_r << 8;
  174.  
  175.   if ( ((f.m.u ^ r.u) & (-g.m.u ^ r.u) & Fix24_msb) && g.m.u )
  176.     f.overflow(r);
  177.   return r;
  178. }
  179.  
  180. Fix48 operator * (Fix48& a, int b)
  181. {
  182.   twolongs r;
  183.   int bpos = (b >= 0);
  184.   unsigned ub = (bpos)? b : -b;
  185.   if ( ub >= 65536L ) {
  186.     r = (bpos)? Fix48_m_max : Fix48_m_min;
  187.     a.range_error(r);
  188.   }
  189.   else {
  190.     unsigned long 
  191.       lo_r = (a.m.l & 0xffff) * ub,
  192.       mi_r = ((a.m.l >> 16) & 0xffff) * ub,
  193.       hi_r = a.m.u * ub;
  194.     r.l = lo_r + (mi_r << 16);
  195.     r.u = hi_r + ((mi_r >> 8) & 0x00ffff00L);
  196.     if ( !bpos ) {
  197.       unsigned long l = r.l;
  198.       r.l = -r.l;
  199.       r.u = ~r.u + ((l ^ r.l) & Fix24_msb ? 0 : Fix24_lsb);
  200.     }
  201.   }
  202.   return r;
  203. }
  204.  
  205. Fix48 operator << (Fix48& a, int b)
  206. {
  207.   twolongs r; r.u = r.l = 0;
  208.   if ( b >= 0 )
  209.     if ( b < 24 ) {
  210.       r.u = (a.m.u << b) + ((a.m.l >> (24 - b)) & 0xffffff00L);
  211.       r.l = a.m.l << b;
  212.     }
  213.     else if ( b < 48 ) {
  214.       r.u = a.m.l << (b - 24);
  215.     }
  216.   return r;
  217. }
  218.  
  219. Fix48 operator >> (Fix48& a, int b)
  220. {
  221.   twolongs r; r.u = r.l = 0;
  222.   if ( b >= 0 )
  223.     if ( b < 24 ) {
  224.       r.l = (a.m.u << (24 - b)) + ((a.m.l >> b) & 0xffffff00L);
  225.       r.u = (a.m.u >> b) & 0xffffff00L;
  226.     }
  227.     else if ( b < 48 ) {
  228.       r.l = (a.m.u >> (b - 24)) & 0xffffff00L;
  229.       r.u = (a.m.u >> 24) & 0xffffff00L;
  230.     }
  231.     else {
  232.       r.l = (a.m.u >> 24) & 0xffffff00L;
  233.       r.u = r.l;
  234.     }
  235.   return r;
  236. }
  237.  
  238. // error handling
  239.  
  240. void Fix24::overflow(long& i)
  241. {
  242.   (*Fix24_overflow_handler)(i);
  243. }
  244.  
  245. void Fix48::overflow(twolongs& i)
  246. {
  247.   (*Fix48_overflow_handler)(i);
  248. }
  249.  
  250. void Fix24::range_error(long& i)
  251. {
  252.   (*Fix24_range_error_handler)(i);
  253. }
  254.  
  255. void Fix48::range_error(twolongs& i)
  256. {
  257.   (*Fix48_range_error_handler)(i);
  258. }
  259.  
  260. // data definitions
  261.  
  262. Fix24_peh Fix24_overflow_handler = Fix24_overflow_saturate;
  263. Fix48_peh Fix48_overflow_handler = Fix48_overflow_saturate;
  264.  
  265. Fix24_peh Fix24_range_error_handler = Fix24_warning;
  266. Fix48_peh Fix48_range_error_handler = Fix48_warning;
  267.  
  268. //function definitions
  269.  
  270. Fix24_peh set_Fix24_overflow_handler(Fix24_peh new_handler) {
  271.   Fix24_peh old_handler = Fix24_overflow_handler;
  272.   Fix24_overflow_handler = new_handler;
  273.   return old_handler;
  274. }
  275.  
  276. Fix48_peh set_Fix48_overflow_handler(Fix48_peh new_handler) {
  277.   Fix48_peh old_handler = Fix48_overflow_handler;
  278.   Fix48_overflow_handler = new_handler;
  279.   return old_handler;
  280. }
  281.  
  282. void set_overflow_handler(Fix24_peh handler24, Fix48_peh handler48) {
  283.   set_Fix24_overflow_handler(handler24);
  284.   set_Fix48_overflow_handler(handler48);
  285. }
  286.  
  287. Fix24_peh set_Fix24_range_error_handler(Fix24_peh new_handler) {
  288.   Fix24_peh old_handler = Fix24_range_error_handler;
  289.   Fix24_range_error_handler = new_handler;
  290.   return old_handler;
  291. }
  292.  
  293. Fix48_peh set_Fix48_range_error_handler(Fix48_peh new_handler) {
  294.   Fix48_peh old_handler = Fix48_range_error_handler;
  295.   Fix48_range_error_handler = new_handler;
  296.   return old_handler;
  297. }
  298.  
  299. void set_range_error_handler(Fix24_peh handler24, Fix48_peh handler48) {
  300.   set_Fix24_range_error_handler(handler24);
  301.   set_Fix48_range_error_handler(handler48);
  302. }
  303.  
  304. void Fix24_overflow_saturate(long& i)
  305.   { i = (i > 0 ? Fix24_m_min : Fix24_m_max); }
  306. void Fix24_ignore(long&) {}
  307. void Fix24_warning(long&)
  308.   { cerr << "warning: Fix24 result out of range\n"; }
  309. void Fix24_overflow_warning_saturate(long& i)
  310.   { cerr << "warning: Fix24 result out of range\n"; 
  311.    Fix24_overflow_saturate(i); }
  312. void Fix24_abort(long&)
  313.   { cerr << "error: Fix24 result out of range\n"; abort(); }
  314.  
  315. void Fix48_ignore(twolongs&) {}
  316. void Fix48_overflow_saturate(twolongs& i)
  317.   { i = (i.u > 0 ? Fix48_m_min : Fix48_m_max); }
  318. void Fix48_warning(twolongs&)
  319.   { cerr << "warning: Fix48 result out of range\n"; }
  320. void Fix48_overflow_warning_saturate(twolongs& i)
  321.   { cerr << "warning: Fix48 result out of range\n"; 
  322.    Fix48_overflow_saturate(i); }
  323. void Fix48_abort(twolongs&)
  324.   { cerr << "error: Fix48 result out of range\n"; abort(); }
  325.  
  326.